package cn.unknowpupil.controller.system;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.common.core.utils.FileUtil;
import cn.unknowpupil.service.utils.ExcelUtil;
import cn.unknowpupil.domain.system.SysDictType;
import cn.unknowpupil.service.system.ISysDictTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * Created at 2020-10-30 by  w10g <br>
 * Initialize SysDictType  ... <br>
 * 字典类型表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-字典类型表模块"}, value = "SysDictType")
@RestController
@RequestMapping(value = "admin/sys-dict-type")
public class SysDictTypeController {

    @Resource
    private ISysDictTypeService SysDictTypeService;

    /**
     * 新增
     *
     * @param SysDictType {@link SysDictType}  字典类型表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "create", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Valid @RequestBody SysDictType SysDictType, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
            return ResultResponse.buildFailure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean created = SysDictTypeService.create(SysDictType);
        if (created) {
            return ResultResponse.success("创建成功");
        } else {
            return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param SysDictType {@link SysDictType}  字典类型表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "update", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Valid @RequestBody SysDictType SysDictType, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
            return ResultResponse.buildFailure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean update = SysDictTypeService.update(SysDictType);
        if (update) {
            return ResultResponse.success("修改成功");
        } else {
            return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 获取
     *
     * @param SysDictTypeId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{SysDictTypeId}")
    public ResultResponse get(@PathVariable("SysDictTypeId") Long SysDictTypeId) {
        SysDictType SysDictType = SysDictTypeService.info(SysDictTypeId);
        return ResultResponse.buildSuccess(SysDictType);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted = SysDictTypeService.delete(ids);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：分页")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysDictType>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysDictType sysDictType) {
        IPage<SysDictType> page = SysDictTypeService.page(current, size, sysDictType);
        return ResultResponse.buildSuccess(page);
    }

    /**
     * 不分页
     *
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：不分页")
    @GetMapping("list")
    public ResultResponse<List<SysDictType>> list() {
        List<SysDictType> page = SysDictTypeService.list();
        return ResultResponse.buildSuccess(page);
    }

    /**
     * 情况字典缓存
     *
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：情况字典缓存")
    @GetMapping("clear")
    public ResultResponse clearCache() {
         SysDictTypeService.clearCache();
        return ResultResponse.success("成功");
    }
    @ApiOperation("系统：导出字典")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResultResponse export(SysDictType dictType) throws IOException {
        List<SysDictType> list = SysDictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        String fileName = util.exportExcel(list, "字典类型");
        System.err.println(fileName);
        return ResultResponse.buildSuccess(fileName);


    }

}
